<?

class Login{

var $username;

var $password;

var $session_id;

var $logged_id;

var $table;

var $fields = array();

var $sessions = array();

var $inputs = array();

var $id_membru;

var $status;

var $tries;



 function Login($t,$f,$s,$in)

    {	

	$this->table = $t;	

	$fld = explode(",",$f);

	for($i=0;$i<count($fld);$i++)

	$this->fields[$i]=trim($fld[$i]);

	

	$ses = explode(",",$s);

	for($i=0;$i<count($ses);$i++)

	$this->sessions[$i]=trim($ses[$i]);

	

	$inp = explode(",",$in);

	for($i=0;$i<count($inp);$i++)

	$this->inputs[$i]=trim($inp[$i]);

	

	if(!isset($_SESSION[$this->sessions[0]]) and !isset($_SESSION[$this->sessions[1]]))

	   {

		if(sizeof($_POST) and isset($_POST[$this->inputs[0]]))

		    {

				$sql = "select * from ".$this->table." where ".$this->fields[0]." = '".$_POST[$this->inputs[0]]."' and ".$this->fields[1]." = '".md5($_POST[$this->inputs[1]])."'";

				$result = mysql_query($sql);

					if(mysql_num_rows($result)){

						$this->logged_in = 1;

						$_SESSION[$this->sessions[0]]=$_POST[$this->inputs[0]];

						$_SESSION[$this->sessions[1]]=md5($_POST[$this->inputs[1]]);

						$this->username = $_POST[$this->inputs[0]];

						$this->password = md5($_POST[$this->inputs[1]]);

						

						$_SESSION['date'] = mysql_fetch_array($result);						

					} else{

						$this->logged_in = 0;

						$this->status = "User sau parola gresita.";

			               }

		   } 

	   } 

	else 

	  {

		$this->logged_in = 1;

	  }

	  if(!isset($_SESSION))

	  $this->logged_in = 0;

   }

   

    function getSession($i){

   return $_SESSION[$this->sessions[$i]];

   }

   

   function getId(){

   return $this->id_membru;

   }

   

   function logout(){

   unset($_SESSION[$this->sessions[0]]);

   unset($_SESSION[$this->sessions[1]]);

   unset($_SESSION['date']);

   return true;

   }

   

   function refreshDate(){

   $sql = "select * from ".$this->table." where ".$this->fields[0]." = '".$_SESSION['date']['username']."'";

   $result = mysql_query($sql);

   $_SESSION['date'] = mysql_fetch_array($result);

   }

   

    function get_time_difference( $start, $end )

    {

    $uts['start']      =    strtotime( $start );

    $uts['end']        =    strtotime( $end );

    if( $uts['start']!==-1 && $uts['end']!==-1 )

    {

        if( $uts['end'] >= $uts['start'] )

        {

            $diff    =    $uts['end'] - $uts['start'];

            if( $days=intval((floor($diff/86400))) )

                $diff = $diff % 86400;

            if( $hours=intval((floor($diff/3600))) )

                $diff = $diff % 3600;

            if( $minutes=intval((floor($diff/60))) )

                $diff = $diff % 60;

            $diff    =    intval( $diff );            

            return( array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) );

        }

        else

        {

            trigger_error( "Ending date/time is earlier than the start date/time", E_USER_WARNING );

        }

    }

    else

    {

        trigger_error( "Invalid date/time data detected", E_USER_WARNING );

    }

    return( false );

}   

}

?>